package com.ruoyi.common.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class ImgUtils {


    public static void main(String[] args) {
//        String imagePath = "D:\\Download\\微信图片_20250430162406.png"; // 替换为你的图片路径
//        String base64String = convertImageToBase64(imagePath);
//        saveBase64ToFile(base64String, "D:\\Download\\base64.txt"); // 替换为保存Base64字符串的文件路径


        String imageUrl = "https://rh-images.xiaoyaoyou.com/01acb4e9d67072c0f9973be7ad23b74f/output/ComfyUI_00003_mgmiz_1746037095.png";
        String base64Image = convertImageUrlToBase64(imageUrl);
        if (base64Image != null) {
            saveBase64ToFile(base64Image, "D:\\Download\\base64.txt"); // 替换为保存Base64字符串的文件路径
        }



//
//        String base64String = "";
//        String outputImagePath = "D:\\Download\\image.jpg"; // 替换为保存图片的路径
//        convertBase64ToImage(base64String, outputImagePath);
    }

    public static String convertImageUrlToBase64(String imageUrl) {
        try {
            // 直接使用原始 imageUrl 构造 URL（确保协议正确，如 http:// 或 https://）
            URL url = new URL(imageUrl);

            // 自动判断 MIME 类型（根据文件扩展名）
            String mimeType = "image/png";
            if (imageUrl.endsWith(".jpg") || imageUrl.endsWith(".jpeg")) {
                mimeType = "image/jpeg";
            } else if (imageUrl.endsWith(".gif")) {
                mimeType = "image/gif";
            }

            try (InputStream inputStream = url.openStream();
                 ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {

                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }

                byte[] imageBytes = outputStream.toByteArray();
                // 使用 Java 17 的 Base64 编码（确保导入 java.util.Base64）
                return "data:" + mimeType + ";base64," + java.util.Base64.getEncoder().encodeToString(imageBytes);

            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void convertBase64ToImage(String base64String, String outputImagePath) {
        try (FileOutputStream fos = new FileOutputStream(new File(outputImagePath))) {
            byte[] imageBytes = Base64.decodeBase64(base64String);
            fos.write(imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String convertImageToBase64(String imagePath) {
        try (FileInputStream fis = new FileInputStream(new File(imagePath))) {
            byte[] imageBytes = new byte[(int) new File(imagePath).length()];
            fis.read(imageBytes);
            return "data:image/png;base64," + Base64.encodeBase64String(imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void saveBase64ToFile(String base64String, String outputFilePath) {
        try (FileOutputStream fos = new FileOutputStream(new File(outputFilePath))) {
            fos.write(base64String.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
